package com.xu.uitls;

import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUtils {
    private static PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();

    static {
        poolingHttpClientConnectionManager.setMaxTotal(100);
        poolingHttpClientConnectionManager.setDefaultMaxPerRoute(10);
    }

    public static String doGet(String url, Map<String,String> params) throws URISyntaxException {
        //通过连接池创建httpClient
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
        //创建配置
        RequestConfig config = RequestConfig.custom().setConnectTimeout(1000)
                .setConnectionRequestTimeout(500)
                .setSocketTimeout(10 * 1000)
                .build();
        //创建返回体
        CloseableHttpResponse response = null;
        //创建返回体，并且设置参数
        URIBuilder uriBuilder = new URIBuilder(url);

        if (params != null){
            for (String key : params.keySet())
                uriBuilder.setParameter(key,params.get(key));
        }


        //创建httpGet对象，并配置config
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        httpGet.setConfig(config);
        //发起请求
        try {
            response = httpClient.execute(httpGet);

            if (response.getStatusLine().getStatusCode() == 200){
                String result = EntityUtils.toString(response.getEntity());
                return result;
            } else {
                return String.valueOf(response.getStatusLine().getStatusCode());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    public static String doPost(String url, Map<String,String> params){
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager).build();
        CloseableHttpResponse response = null;
        HttpPost httpPost = new HttpPost(url);
        RequestConfig config = RequestConfig.custom().setConnectTimeout(1000)
                .setConnectionRequestTimeout(500)
                .setSocketTimeout(10 * 1000)
                .build();
        httpPost.setConfig(config);
        List<NameValuePair> postParams = new ArrayList<>();

        if (params != null){
            for (String key : params.keySet())
                postParams.add(new BasicNameValuePair(key,params.get(key)));
        }

        try {
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(postParams, "utf-8");
            httpPost.setEntity(urlEncodedFormEntity);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
            response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() == 200){
                String result = EntityUtils.toString(response.getEntity());
                return result;
            } else{
                return String.valueOf(response.getStatusLine().getStatusCode());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}
